<template>
  <div class="view-fill">
    <div v-if="!modifyInfo.visible" class="app-container-flex">
      <NewFullScreen
        v-model="showDialog"
        :title="title"
        :needOperateAffair="true"
        :operateAffairData="affairTableInfo"
        @returnBack="returnBack"
      >
        <template v-slot:full-button>
          <el-button
            v-if="
              $permission('wmsExportStockOrder:createDownShelf') &&
              infoList.data.status === 4
            "
            @click="showDownShelf"
            >生成拣货任务</el-button
          >
          <el-button
            v-if="
              $permission('wmsExportStockOrder:quickDownShelf') &&
              infoList.data.status === 4
            "
            @click="quickDownShelf"
            >快捷拣货</el-button
          >
          <el-button
            v-if="
              ($permission('wmsExportStockOrder:export') &&
                infoList.data.status === 5) ||
              infoList.data.status === 6
            "
            @click="exportOrder"
            >出库</el-button
          >
          <PopoverPrint
            ref="popoverShow"
            v-model:popoverVisible="popoverVisible"
            :canPrint="canPrint"
            :templateArr="templateArr"
            @tempClick="tempClick"
            @print="print"
          ></PopoverPrint>
          <!-- <el-button
            v-if="$permission('wmsExportStockOrder:printOrderDetail')"
            @click="print"
            >打印</el-button
          > -->
          <el-button
            v-if="$permission('wmsExportStockOrder:excelOrderDetail')"
            @click="exportExcel"
            >导出excel</el-button
          >
        </template>
        <div class="model-panel">
          <div class="model-panel-title">
            <p>{{ $t('basic_information') }}</p>
          </div>
          <div class="model-panel-body">
            <BasicInformation :info="infoList"></BasicInformation>
          </div>
        </div>
        <div class="model-panel">
          <div class="model-panel-title">
            <p>{{ $t('detailInfo') }}</p>
          </div>
          <div class="model-panel-body">
            <div class="layui-card-body">
              <!-- 表格 -->
              <PageTable
                :refresh="tableInfo.refresh"
                :init-curpage="tableInfo.initCurpage"
                v-model:data="tableInfo.data"
                :pager="false"
                :field-list="tableInfo.fieldList"
                :handle="tableInfo.handle"
                :check-box="false"
                :tab-index="true"
                :isDynamic="true"
              >
              </PageTable>
            </div>
          </div>
        </div>
      </NewFullScreen>
    </div>
    <IndividualDetails
      v-if="showIndividualDetails"
      v-model:show-dialog="showIndividualDetails"
      v-model:query="paramsIndividual"
      :info="productInfo"
    ></IndividualDetails>
    <wms-export-stock-order-modify
      v-if="modifyInfo.visible"
      v-model:visible="modifyInfo.visible"
      :title="modifyInfo.title"
      :export-id="modifyInfo.exportId"
      @returnModify="returnModify"
    />
    <select-users
      v-if="userInfo.visible"
      @callback="callBackUser"
      v-model:showDialog="userInfo.visible"
      :query="userInfo.query"
      :checkBox="false"
      :saveEmpty="true"
      title="选择拣货人"
      width="600px"
      :api="userInfo.api"
      :highlightRow="true"
    />
  </div>
</template>

<script setup name="wmsExportStockDetails">
import { getCustomizeSetting } from '@/api/customize'
import wmsExportStockOrderModify from './modify'
import {
  excelWmsExportStockOrder,
  getWmsExportStockById,
  getWmsExportStockDetailCustomizeListAll,
  printWmsExportStockOrder,
  exportWmsExportOrder,
  createDownShelfTaskWmsExportOrder,
} from '@/api/wms.js'
import { fatchTemplateByCode, getRrgUserPage } from '@/api/base'
import { orderOpAffairAll } from '@/api/affair'
import IndividualDetails from '@/views/wms/exportStock/IndividualDetails'
import { doPrint } from '@/utils/exportUtil.js'
import { selectByCodeAndDept } from '@/api/report.js'
import { updatePrintCount } from '@/api/mapping'

const { proxy } = getCurrentInstance()
const props = defineProps({
  visible: {
    type: Boolean,
    default: false,
  },
  tableId: {
    type: [Boolean, String],
  },
})
const title = ref('出库单详情')
const productInfo = ref('')
const infoList = ref({
  data: {},
  fieldList: [
    {
      label: 'wms出库单号',
      value: 'orderNo',
    },
    {
      label: '出库仓库',
      value: 'depotName',
    },
    {
      label: '耗材品类数',
      value: 'productQty',
    },
    {
      label: '耗材总数量',
      value: 'totalQty',
    },
    {
      label: '已拣货数量',
      value: 'downSelfQty',
    },
    {
      label: '状态',
      value: 'statusValue',
    },
    {
      label: '出库人',
      value: 'exportUserName',
    },
    {
      label: '出库时间',
      value: 'exportTime',
    },
    {
      label: '创建人',
      value: 'createUserName',
    },
    {
      label: '创建时间',
      value: 'createTime',
    },
    {
      label: '备注',
      value: 'remark',
      type: 'popover',
    },
  ],
})
const showIndividualDetails = ref(false)
const paramsIndividual = ref({})
const tableInfo = ref({
  refresh: 1,
  initCurpage: 1,
  data: [],
  fieldList: [],
  handle: {
    fixed: 'right',
    label: '操作',
    width: '80',
    btList: [
      {
        label: '个体详情',
        event: individualDetails,
        show: true,
        ifRender: ifIndividualDetails,
      },
    ],
  },
})
const filterInfo = ref({
  query: {
    customizeUserVO: {},
    queryMap: {
      orderId: {
        queryType: 'EQUALS',
        queryCategory: 'AND',
        value: props.tableId,
      },
    },
    queryParams: {},
  },
})
const modifyInfo = ref({
  visible: false,
  title: '',
})
const userInfo = ref({
  visible: false,
  api: getRrgUserPage,
  id: '',
  query: {
    idNotIn: '',
    depotAuth: '',
    status: 1,
    isTransfer: 1,
  },
})
const affairTableInfo = ref({
  api: orderOpAffairAll,
  query: {
    //数字字典单据类型 资产转移：74
    orderType: 75,
    orderId: props.tableId,
    sort: 'operate_time desc',
  },
})
const canPrint = computed(() => {
  return proxy.$permission('wmsExportStockOrder:printOrderDetail')
})
const showDialog = computed({
  get() {
    return props.visible
  },
  set(newValue) {
    // 更新父组件visible
    proxy.$emit('update:visible', newValue)
  },
})
const canUpdate = computed(() => {
  return (
    proxy.$permission('wmsExportStockOrder:update') &&
    infoList.value.data.status === 1
  )
})
function returnModify() {
  getList()
  getMain()
}
function ifIndividualDetails(row) {
  return (
    infoList.value.data.status === 7 ||
    (infoList.value.data.status === 10 && row.downShelfQty > 0) ||
    (infoList.value.data.status === 5 && row.downShelfQty > 0)
  )
}
//导出excel
async function exportExcel() {
  excelWmsExportStockOrder(filterInfo.value.query).then((resp) => {
    if (resp) {
      proxy.$fn.parseExcel(resp)
    }
  })
}
const templateArr = ref([])
const popoverVisible = ref(false)
//打印
function print() {
  selectByCodeAndDept({
    tempCode: 'wmsExportDetail',
    deptId: infoList.value.data.deptId,
  }).then((res) => {
    if (res && res.success) {
      templateArr.value = res.data
      popoverVisible.value = true
      // 一条数据则直接打印
      if (templateArr.value.length === 1) {
        proxy.$refs.popoverShow.hidePopover()
        doPrint({
          name: templateArr.value[0].ureportName,
          params: {
            orderId: props.tableId,
          },
          callback: printCallback,
          data: {},
        })
      }
    }
  })
}
function printCallback() {
  try {
    // 打印成功增加打印次数
    updatePrintCount({
      orderNo: infoList.value.data.orderNo,
      orderId: props.tableId,
      orderType: 64,
    })
  } catch (error) {}
}
function tempClick(item) {
  doPrint({
    name: item.ureportName,
    params: {
      orderId: props.tableId,
    },
    callback: printCallback,
    data: {},
  })
  popoverVisible.value = false
}
function getPrintData(temp) {
  let params = {
    tableKey:
      'wmsExportStockDetails-com.slip.spd.entity.vo.wms.WmsExportStockDetailVO',
    orderIds: [props.tableId],
  }
  printWmsExportStockOrder(params).then((res) => {
    if (res.success && res) {
      let data = proxy.$fn.deepClone(res.data)
      let newData = data.map((item) => {
        return {
          ...item,
          ...item.orderInfo,
        }
      })
      proxy.$lodop.preview(temp, newData)
    }
  })
}
//个体详情
function individualDetails(row) {
  paramsIndividual.value['orderDetailId'] = {
    queryType: 'IN',
    queryCategory: 'AND',
    value: row.groupConcatIds,
  }
  productInfo.value = row
  showIndividualDetails.value = true
}
function getList() {
  getWmsExportStockDetailCustomizeListAll(filterInfo.value.query).then(
    (resp) => {
      if (resp && resp.success) {
        tableInfo.value.data = resp.data
      }
    }
  )
}

function exportOrder() {
  exportWmsExportOrder({ id: infoList.value.data.id }).then((resp) => {
    if (resp && resp.success) {
      getMain()
      getList()
    }
  })
}

function createDownShelf() {
  createDownShelfTaskWmsExportOrder(formUserInfo.value.data).then((resp) => {
    if (resp && resp.success) {
      getMain()
      getList()
      handleClose()
    }
  })
}

function showDownShelf() {
  userInfo.value.visible = true
  userInfo.value.id = infoList.value.data.id
  userInfo.value.query.depotAuth = infoList.value.data.depotId
}
function callBackUser(data) {
  let params = {
    id: userInfo.value.id,
    downSelfUser: data.id,
  }
  createDownShelfTaskWmsExportOrder(params).then((resp) => {
    if (resp && resp.success) {
      getMain()
      getList()
    }
  })
}

function returnBack() {
  proxy.$emit('returnBack', false)
}

function quickDownShelf() {
  modifyInfo.value.exportId = infoList.value.data.id
  modifyInfo.value.title = '新增WMS快捷拣货'
  modifyInfo.value.visible = true
}

function getMain() {
  getWmsExportStockById(props.tableId).then((resp) => {
    if (resp && resp.success) {
      infoList.value.data = resp.data
    }
  })
}
function getCustomize() {
  let param = {
    tableKey:
      'wmsExportStockDetails-com.slip.spd.entity.vo.wms.WmsExportStockOrderDetailVO',
  }
  getCustomizeSetting(param).then((res) => {
    if (res && res.success) {
      let data = res.data
      filterInfo.value.query.customizeUserVO = data
      let obj = proxy.$fn.setFieldList(
        tableInfo.value.fieldList,
        data.detail,
        true
      )
      tableInfo.value.fieldList = obj.fieldList
      getMain()
      getList()
    }
  })
}

let obj = {
  orderNo: {
    label: '单号',
    value: 'orderNo',
    componentType: 'input',
    show: false,
    hidden: true,
  },
  locationTypeValue: {
    label: '货位属性',
    value: 'locationTypeValue',
    asValue: 'locationType',
    componentType: 'select-model',
    code: 'goods_location_type',
    show: false,
    hidden: true,
  },
}
tableInfo.value.fieldList = Object.values({
  ...proxy.$fn.deepClone(proxy.$fieldfList.FieldList),
  ...obj,
})
getCustomize()
</script>

<style scoped></style>
